package com.hcb.tonguetiphealth.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hcb.tonguetiphealth.model.Ordered;
import com.hcb.tonguetiphealth.model.OrderedGroup;
import com.hcb.tonguetiphealth.view.R;

public class OrderedExpandableListAdapter extends BaseExpandableListAdapter {

	private List<OrderedGroup> groupList;
	private Context mContext;
    
	public OrderedExpandableListAdapter(Context mContext, List<OrderedGroup> groupList) {
		this.mContext = mContext;
		this.groupList = groupList;
	}
	public Object getChild(int groupPosition, int childPosition) {
		return groupList.get(groupPosition).getChildList().get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		Ordered ordered = groupList.get(groupPosition).getChildList().get(childPosition);
        
		View view = convertView;
		
        if(view == null){
            LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.homepage_personal_ordered_list_item, null);
        }
        TextView tv_remark = (TextView)view.findViewById(R.id.homepage_ordered_remark);
		TextView tv_time = (TextView)view.findViewById(R.id.homepage_ordered_time);
		TextView tv_price = (TextView)view.findViewById(R.id.homepage_ordered_price);

		tv_remark.setText(ordered.getRemark());
		tv_time.setText(ordered.getTime());
		tv_price.setText(ordered.getPrice() + " 元");
	
		return view;
	}

	public int getChildrenCount(int groupPosition) {
		
		return groupList.get(groupPosition).getChildList().size();
	}

	public Object getGroup(int groupPosition) {
		return groupList.get(groupPosition);
	}

	public int getGroupCount() {
		return groupList.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View groupView = convertView;

		if(groupView == null) {
			LayoutInflater inflater_new = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			groupView = inflater_new.inflate(R.layout.group , null);
		}
		TextView text = (TextView)groupView.findViewById(R.id.group_name);
		ImageView image=(ImageView)groupView.findViewById(R.id.group_img);
	
		if(isExpanded) {
			image.setImageResource(R.drawable.close_more);
		} else {
			image.setImageResource(R.drawable.open_more);
		}
		groupView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.FILL_PARENT, 80));
		if(groupPosition % 2 == 0) {
			groupView.setBackgroundResource(R.drawable.xml_linearlayout_background);
		} else {
			groupView.setBackgroundResource(R.drawable.xml_linearlayout_background2);
		}
		
		text.setText(groupList.get(groupPosition).getGroupName());

		return groupView;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
 
}
